package display {
	import flare.vis.Visualization;
	import flare.vis.data.DataSprite;
	import flare.vis.data.EdgeSprite;
	import flare.vis.data.NodeSprite;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	
	import org.aswing.AsWingManager;
	import org.aswing.BoxLayout;
	import org.aswing.JComboBox;
	import org.aswing.JPanel;
	import org.aswing.event.SelectionEvent;
	
	public class SwitchViewSprite extends Sprite {
		//		public var viewText:Array = [];
		public var dropDownList:JComboBox;
		private var trash:Object = [null, null, null, null];
		private var vis_:Visualization;
		public function SwitchViewSprite(vis:Visualization, stage:Stage) {
			super();
			vis_ = vis;
			AsWingManager.setRoot(stage);
			dropDownList = new JComboBox();
			var text:Array = [
				'Gmail Friend View',
				'Yahoo Friend View',
				'MSN Friend View',
				'Full Friend View'
			];
			dropDownList.setListData(text);
			dropDownList.addSelectionListener(onSelect);
			var panel:JPanel = new JPanel(new BoxLayout(BoxLayout.Y_AXIS));
			panel.setX(0);
			panel.setY(500);
			panel.setSizeWH(100, 50);
			panel.append(dropDownList);
			
			
			//			this.addChild(dropDownList);
			//			dropDownList.showPopup();
		}
		private function onSelect(event:SelectionEvent):void {
			var index:int = event.getLastIndex();
			switch (index) {
				case 0:
					break;
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		}
		private function swithToFullView():void {
			for (var i = 0; i < 3; ++i) {
				restoreFromTrash(trash[i]);
			}
		}
		private function switchToGmailView():void {
			if (trash[0]) {
				restoreFromTrash(0, trash[0]);
			}
			if (trash[1] == null) {
				removeToTrash(1, trash[1]);
			}
			if (trash[2] == null) {
				removeToTrash(2, trash[2]);
			}
		}
		private function switchToYahooView():void {
			if (trash[1]) {
				restoreFromTrash(1, trash[1]);
			}
			if (trash[0] == null) {
				removeToTrash(0, trash[0]);
			}
			if (trash[2] == null) {
				removeToTrash(2, trash[2]);
			}
		}
		private function switchToMSNView():void {
			if (trash[2]) {
				restoreFromTrash(2, trash[2]);
			}
			if (trash[0] == null) {
				removeToTrash(0, trash[0]);
			}
			if (trash[1] == null) {
				removeToTrash(1, trash[1]);
			}
		}
		private function restoreFromTrash(t:Object):void {
			if (t.nodes) {
				for each (var n:DataSprite in t.nodes) {
					vis_.data.nodes.add(n);
				}
			}
			if (t.edges) {
				for each (var n:DataSprite in t.edges) {
					vis_.data.edges.add(n);
				}
			}
			t = null;
		}
		private function removeToTrash(group:int, t:Object) {
			t = {nodes: [], edges: []};
			for each (var edge:EdgeSprite in vis_.data.edges) {
				if (edge.source.data.group == group || edge.target.data.group == group) {
					vis_.data.edges.remove(edge);
					t.edges.push(edge);
				}
			}
			for each (var node:NodeSprite in vis_.data.nodes) {
				if (node.data.group == group) {
					vis_.data.nodes.remove(node);
					t.nodes.push(node);
				}
			}
		}
		public function update(text:Array):void {
			dropDownList.setListData(text);
		}
	}
}
